/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.order.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.account.entity.Payment;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.BomItemService;
import com.rzico.basics.service.MemberService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.entity.*;
import com.rzico.order.enumx.OrderPaymentStatusEnum;
import com.rzico.order.model.TrackingNoVo;
import com.rzico.order.plugin.JDPluginService;
import com.rzico.order.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物流配送接口
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-17
 */
@Api(description = "物流配送接口")
@RestController
@RequestMapping("/member/shipping")
public class ShippingController extends BaseController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ShippingService shippingService;

    @Autowired
    private ShippingItemService shippingItemService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private BomItemService bomItemService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderPlanService orderPlanService;

    @Autowired
    private JDPluginService jDPluginService;

    /**
     * 物流查询,快递100接口
     *
     * @param trackingNo
     * @return
     */
    @ApiOperation("物流查询,快递100接口")
    @PostMapping("/delivery_query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "trackingNo", value = "物流单号", dataType = "String", paramType = "query")
    })
    public CommResult<Map> delivery_query(String trackingNo) {
        return CommResult.success(shippingService.deliveryQuery(trackingNo));
    }


    /**
     * 套餐物流查询
     *
     * @param orderId
     * @param productId
     * @return
     */
    @ApiOperation("套餐物流查询")
    @PostMapping("/order_plan_query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "String", paramType = "query")
    })
    public CommResult<OrderPlan> order_plan_query(Long orderId,Long productId) {
        Order order = orderService.selectByPrimaryKey(orderId);
        List<OrderPlan> orderPlanList = orderPlanService.getItemList(order.getEnterpriseId(),orderId,productId);
        for (OrderPlan orderPlan:orderPlanList) {
           orderPlan.setItemList(bomItemService.getItemList(orderPlan.getBomId(),String.valueOf(orderPlan.getSeqno())));
        }
        return CommResult.success(orderPlanList);
    }


    /**
     * 多包巢查询
     *
     * @param orderId
     * @return
     */
    @ApiOperation("多包巢查询")
    @PostMapping("/trackingNo_query")
    public CommResult<ShippingItem> trackingNo_query(Long orderId, Pageable pageable) {

        pageable.setPageSize(10000);

        Order order = orderService.selectByPrimaryKey(orderId);

        //获取套餐信息
        List<TrackingNoVo> orderPlanList = new ArrayList<>();
        List<OrderItem> orderItemList = orderItemService.getItemList(orderId);
        for (OrderItem orderItem:orderItemList) {
            if (orderItem.getSubType().equals(1)) {
                TrackingNoVo vo = new TrackingNoVo();
                vo.setName(orderItem.getName());
                vo.setSn(orderItem.getSn());
                vo.setPrice(orderItem.getPrice());
                vo.setProductId(orderItem.getProductId());
                vo.setShipped(BigDecimal.ZERO);
                vo.setUnshipped(BigDecimal.ZERO);
                vo.setSubType(1);
                vo.setSpec(orderItem.getSpec());
                vo.setThumbnail(orderItem.getThumbnail());
                vo.setUnit(orderItem.getUnit());
                //送货检查是否有用掉
                Map<String, Object> params = new HashMap<>();
                params.put("status",2);
                params.put("enterpriseId", order.getEnterpriseId());
                params.put("productId", orderItem.getProductId());
                params.put("orderId", order.getId());
                Integer shipped = orderPlanService.selectRowCount(params);
                vo.setShipped(new BigDecimal(shipped));

                params.clear();
                params.put("enterpriseId", order.getEnterpriseId());
                params.put("productId", orderItem.getProductId());
                params.put("orderId", order.getId());
                Integer total = orderPlanService.selectRowCount(params);

                vo.setUnshipped(new BigDecimal(total).subtract(vo.getShipped()));

                //判断是否配送中

                params.clear();
                params.put("status",1);
                params.put("enterpriseId", order.getEnterpriseId());
                params.put("productId", orderItem.getProductId());
                params.put("orderId", order.getId());
                Integer ing = orderPlanService.selectRowCount(params);

                if (ing>0) {
                   vo.setTrackingNo("配送中");
                } else {
                    if (vo.getUnshipped().compareTo(BigDecimal.ZERO)>0) {
                        vo.setTrackingNo("待配送");
                    } else {
                        vo.setTrackingNo("已完成");
                    }
                }

                orderPlanList.add(vo);
            }
        }


        //获取物流信息
        Map<String,Object> params = new HashMap<>();
        params.put("sortField","tracking_no");
        params.put("sortType","desc");
        params.put("orderId",orderId);

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<TrackingNoVo> list = shippingItemService.getTrackingNoVoList(params);

        orderPlanList.addAll(list);
        //查询时，获取京东商品物流信息
        //jDPluginService.updateShippingItemTrackno(list);
        PageResult<TrackingNoVo> pageResult = new PageResult<TrackingNoVo>(orderPlanList, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }


}
